package myJs.tool

import japgolly.scalajs.react.vdom.all.VdomAttr
import myJs.myPkg.jquery.$
import org.scalajs.dom.{Blob, BlobPropertyBag, window}

import scala.scalajs.js.{Date, |}
import myJs.Implicits._
import myJs.myPkg._
import org.scalajs.dom.window.localStorage
import scalatags.Text.all._

import scala.scalajs.js
import scala.scalajs.js.JSConverters._
import scala.scalajs.js.typedarray.Uint8Array


object Tool extends ProjectTool {

  val dataToggle=VdomAttr("data-toggle")

  val pattern = "yyyy-mm-dd"

  val zhInfo = "信息"
//  val layerOptions = LayerOptions(title = zhInfo, closeBtn = 0, skin = "layui-layer-molv", btn = List())

  val phoneRegexp = js.RegExp(
    "^((00|\\+)?(86(?:-| )))?((\\d{11})|(\\d{3}[- ]{1}\\d{4}[- ]{1}\\d{4})|((\\d{2,4}[- ]){1}(\\d{7,8}|(\\d{3,4}[- ]{1}\\d{4}))([- ]{1}\\d{1,4})?))$"
  )
  val integerRegexp = js.RegExp("^(?:-?(?:0|[1-9][0-9]*))$")

  val zhRunning = "正在运行"
//  val myElement = div(id := "content")(
//    span(id := "info")(zhRunning),
//    " ",
//    img(src := s"${Api.images}/running2.gif", width := 30, height := 20, cls := "runningImage")
//  ).render

//  def myElement(content:String)={
//    div(id := "content")(
//      span(id := "info")(content),
//      " ",
//      img(src := s"${Api.images}/running2.gif", width := 30, height := 20, cls := "runningImage")
//    ).render
//  }

  val confirmButtonClass = "btn-danger"
  val cancelButtonClass = "green"

  def betweenValidate(min: Int, max: Int, info: String) = (v: String) => {
    val valid = v.isDouble && v.toDouble >= min && v.toDouble <= max
    val rs = if (valid) {
      valid
    } else s"${info}必须处于${min}到${max}之间！"
    rs.asInstanceOf[Boolean | String]
  }

  def timeBefore = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() <= dbDate.getTime()
  }

  def timeAfter = (dateStr: String, dbDateStr: String) => {
    val date = new Date(dateStr)
    val dbDate = new Date(dbDateStr)
    dbDate.setUTCHours(0, 0, 0, 0)
    date.getTime() >= dbDate.getTime()
  }

  def b2Display(b: Boolean) = {
    if (b) "block" else "none"
  }

  def getAccount = {
    localStorage.getItem("account")
  }

  def getToken = {
    localStorage.getItem("token")
  }

  def getInputByName(name: String) = {
    $(s":input[name='${name}']")
  }

  def formatDate(time: Date) = {
    val year = time.getFullYear().toInt
    val month = time.getMonth().toInt + 1
    val day = time.getDate().toInt
    s"${year}-${zeroFill(month)}-${zeroFill(day)}"
  }

  def zeroFill(int: Int) = {
    if (int < 10) s"0${int}" else int.toString
  }

  def formatDateTime(time: Date, showYear: Boolean = true, showSecond: Boolean = true) = {
    val year = time.getFullYear().toInt
    val month = time.getMonth().toInt + 1
    val day = time.getDate().toInt
    val hour = time.getHours().toInt
    val minute = time.getMinutes().toInt

    val secondStr = if (showSecond) {
      val second = time.getSeconds().toInt
      s":${zeroFill(second)}"
    } else ""
    if (showYear) {
      s"${year}-${zeroFill(month)}-${zeroFill(day)} ${zeroFill(hour)}:${zeroFill(minute)}${secondStr}"
    } else {
      s"${zeroFill(month)}-${zeroFill(day)} ${zeroFill(hour)}:${zeroFill(minute)}${secondStr}"
    }
  }

  def base642binaryArray(content: String) = {
    val byteCharacters = window.atob(content)
    val sliceSize = 512
    val byteArrays = byteCharacters.grouped(sliceSize).map { slice =>
      val byteNumbers = slice.map(_.toShort).toJSArray
      new Uint8Array(byteNumbers)
    }
    byteArrays.toJSArray.map(_.asInstanceOf[js.Any])
  }

  def dbFmt(v: String) = {
    if (v.isDouble) {
      ujson.Num(v.toDouble)
    } else {
      ujson.Str(v)
    }
  }

  def getCurrentTime = {
    val myDate = new Date()
    val year = myDate.getFullYear()
    val month = myDate.getMonth() + 1
    val date = myDate.getDate()
    val h = myDate.getHours()
    val m = myDate.getMinutes()
    val s = myDate.getSeconds()
    s"${year}_${getNow(month)}_${getNow(date)}_${getNow(h)}_${getNow(m)}_${getNow(s)}"
  }

  def getNow(s: Double) = {
    if (s < 10) s"0${s.toInt}" else s"${s.toInt}"
  }

}
